package org.colour.amber.rest;

import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.io.IOException;
import java.net.URI;
import java.text.ParseException;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.colour.amber.enums.ContactType;
import org.colour.amber.rest.api.ContactInfo;
import org.colour.amber.rest.api.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * @author jasonfu
 * @date 2024/9/14 10:11
 */
@Slf4j
@Tag(name = "用户服务", description = "用于演示最基本的Restful风格的API")
@RestController
@RequestMapping(UserRestController.PATH_USER)
public class UserRestController {

    public static final String PATH_USER = "/api/v1/user";

    @Operation(summary = "用户服务-GET方式使用路径传参", description = "用户服务-GET方式使用路径传参")
    @GetMapping("/{name}")
    public User get(@PathVariable String name) throws ParseException {
        log.info("name={}", name);
        // 这个日期是符合夏令时问题的，顺便验证时区是否正确
        Date birthday = DateUtils.parseDate("1990-05-22 00:00:00", "yyyy-MM-dd HH:mm:ss");
        ContactInfo phone = new ContactInfo(ContactType.PHONE, "18538182601");
        ContactInfo email = new ContactInfo(ContactType.EMAIL, "fuyongde@foxmail.com");
        return new User(name, 18, birthday, Lists.newArrayList(phone, email));
    }

    @Operation(summary = "用户服务-GET方式使用form表单传参", description = "用户服务-GET方式使用form表单传参")
    @GetMapping
    public User get(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer age,
            @RequestParam(required = false) Date birthday) {
        log.info("name={}|age={}|birthday={}", name, age, birthday);
        return new User(name, age, birthday, Lists.newArrayList());
    }

    @Operation(summary = "用户服务-POST方式使用body传参", description = "用户服务-POST方式使用body传参")
    @PostMapping
    @ApiResponse(responseCode = "201")
    public ResponseEntity<User> postBody(@Valid @RequestBody User user, UriComponentsBuilder uriBuilder) {
        log.info("name={}|age={}|birthday={}", user.name(), user.age(), user.birthday());
        URI uri = uriBuilder.path(PATH_USER + "/" + user.name()).build().toUri();
        log.info("uri={}", uri);
        return ResponseEntity.created(uri).body(user);
    }

    @Operation(summary = "用户服务-PUT方式使用body传参", description = "用户服务-PUT方式使用body传参")
    @PutMapping
    public User putBody(@RequestBody User user) {
        log.info("name={}|age={}|birthday={}", user.name(), user.age(), user.birthday());
        return user;
    }

    @Operation(summary = "用户服务-DELETE方式使用路径传参", description = "用户服务-DELETE方式使用路径传参")
    @DeleteMapping("/{name}")
    public String delete(@PathVariable String name) {
        log.info("name={}", name);
        return "SUCCESS";
    }

    @Operation(summary = "用户服务-POST方式使用路径传参+上传文件", description = "用户服务-POST方式使用路径传参+上传文件")
    @PostMapping("/{name}/header")
    public String postHeader(@PathVariable String name, @RequestParam MultipartFile file) throws IOException {
        log.info("name={}|fileName={}", name, file.getOriginalFilename());
        byte[] bytes = file.getBytes();
        String msg = new String(bytes);
        log.info("fileContent={}", msg);
        return "SUCCESS";
    }
}
